package com.toilet.easyvideofx.util;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

/**
 * @description ip工具类
 */
public class IPUtil {

    /**
     * 获取本机ip
     *
     * @return ip列表
     */
    public static List<String> getIPs() {
        List<String> ipList = new ArrayList<>();
        try {
            Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
            while (interfaces.hasMoreElements()) {
                NetworkInterface networkInterface = interfaces.nextElement();
                if (networkInterface.isUp() && !networkInterface.isLoopback()) {
                    Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
                    while (addresses.hasMoreElements()) {
                        InetAddress address = addresses.nextElement();
                        if (!address.isLoopbackAddress() && address.isSiteLocalAddress()) {
                            ipList.add(address.getHostAddress());
                        }
                    }
                }
            }
            return ipList;
        } catch (SocketException e) {
            throw new RuntimeException(e);
        }
    }
}
